<!DOCTYPE html>
<html><head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate">
  <title>python-opencv实现人脸检测 &middot; Dragon Blog</title>
  <meta name="keywords" content="素锦, inspiration, customization, rainmeter, design, web, 壁纸, 设计, 收集, wallpaper, collection, jaku, icon">
  <meta name="description" content="世界上每个角落都有人过着相似的人生">
  <meta name="author" content="CodeLong">
  <link rel="icon" type="image/png" href="">
  <link rel="stylesheet" href="/css/diaspora.css">
  <link rel="stylesheet" href="/css/insight.css">
  <link rel="stylesheet" href="/css/custom.css">
</head><body class="loading">
        <div id="loading"></div>
				<div id="nav"></div>
				<div class="nav-user"></div>
    <div id="single">
    <div id="top" style="display: block;">
        <div class="bar">
        </div>
        <a class="icon-icon" href="javascript:history.back()">
        </a>
        <div title="播放/暂停" class="icon-play">
        </div>
        
        <h3 class="subtitle" style="display: none;">
        python-opencv实现人脸检测</h3>
        <div class="social">
            <div>
                <div class="share">
                    <a title="获取二维码" class="icon-wechat" href="javascript:;"></a>
                </div>
                <div id="qr"></div>
            </div>
        </div>
        <div class="scrollbar" style="width: 1.1636%;"></div>
    </div>
    <div class="section">
        <div class="article">
            <div>
                <h1 class="title">
                python-opencv实现人脸检测</h1>
                <div class="stuff">
                    
                    <span>December 25, 2019</span>
                    <span>字数 282</span>
                    
                    
                </div>
                <div class="content">
                    

<h1 id="python-opencv-mjpg-streamer树莓派实现人脸识别">python-opencv+mjpg-streamer树莓派实现人脸识别</h1>

<p>只用opencv2版本就可以了，亲测可用！！！</p>

<p>废话不多说，直接上代码！</p>

<pre><code>import cv2
#mjpg-streamer的本地录像
cap = cv2.VideoCapture(
    r'./yidong.avi' 
)
#导入已经下载好的人脸模型
classfier = cv2.CascadeClassifier(
    r'./haarcascade_frontalface_alt2.xml' 
)               
color = (0, 255, 0)
count = 0
while cap.isOpened():
    ok, frame = cap.read()  
    if not ok:
        break
#转化为灰度图像
    grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#检测人脸特征
    faceRects = classfier.detectMultiScale(
        grey,
        scaleFactor=1.25,
        minNeighbors=3,
        minSize=(35, 35) 
    )
#检测人脸特征值大于0就进行截图
    if len(faceRects) &gt; 0:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        for faceRect in faceRects:  
            x, y, w, h = faceRect
            cv2.rectangle(frame, (x , y), (x + w, y + h), color, 2)
            f = cv2.resize(gray[y:y+h, x:x+w], (200,200))
            cv2.imwrite('./student/%s.jpg' % str(count) , f)
            count +=1
        if count &gt; 100 :
            break     
#显示图像窗口 
    cv2.imshow(&quot;Face Detection&quot;, frame)
    c = cv2.waitKey(5)		
    if c &amp; 0xFF == ord('q'):
        break
#释放资源
cap.release()
cv2.destroyAllWindows()
</code></pre>

<p>结果截图：
<img src="https://note.youdao.com/yws/api/personal/file/WEBf3bd16d6115c853f6c25bfa5ec6dec70?method=download&amp;shareKey=4032bfab7365d9142860dfde935b715f" alt="" />
<img src="https://note.youdao.com/yws/api/personal/file/WEB0645477f96b48c8f49b3f1d5b09615b9?method=download&amp;shareKey=7c93011f4164b8066394e4590ea54fec" alt="" /></p>

<p>Author：「CodeLong」<br>
BGM：告白之夜</p>

                    
                    <audio id="audio" loop="1" preload="auto" style="width: 100%;" controls="controls">
                        <source type="audio/mpeg" src="/mp3/%e5%91%8a%e7%99%bd%e4%b9%8b%e5%a4%9c.mp3">
                        <a href="/mp3/%e5%91%8a%e7%99%bd%e4%b9%8b%e5%a4%9c.mp3">/mp3/告白之夜.mp3</a>
                    </audio>
                </div>
                <div class="comment-wrap">

                </div>
            </div>
        </div>
    </div>
    <div class="relate">
        <ul>
            <h3 id="prev_next">
                <em>相 关 文 章</em>
                <span>
                    <a href="javascript: window.scrollTo(0, 0);">
                    返回顶部</a>
                    
                        <a href="https://codelong2010.github.io/posts/%E5%B5%8C%E5%85%A5%E5%BC%8F%E9%80%9A%E4%BF%A1%E5%AE%9E%E9%AA%8C/" rel="prev">上一篇</a>
                    
                    
                </span>
            </h3>
            
            
            
                
                    
                
                    
                    <li>
                        <div>
                            <a class="relatea" href="https://codelong2010.github.io/posts/%E5%B5%8C%E5%85%A5%E5%BC%8F%E9%80%9A%E4%BF%A1%E5%AE%9E%E9%AA%8C/" title="嵌入式Server和Client通信实验">
                            嵌入式Server和Client通信实验</a>
                            <p>喜欢我的小伙伴，请多点关注我哦！...</p>
                        </div>
                        
                        <a href="https://codelong2010.github.io/posts/%E5%B5%8C%E5%85%A5%E5%BC%8F%E9%80%9A%E4%BF%A1%E5%AE%9E%E9%AA%8C/" title="嵌入式Server和Client通信实验">
                            <img class="relateimg" src="/img/1.jpg" alt="嵌入式Server和Client通信实验">
                        </a>
                    </li>
                    
                
                    
                    <li>
                        <div>
                            <a class="relatea" href="https://codelong2010.github.io/posts/%E6%AC%A2%E8%BF%8E%E5%A4%A7%E5%AE%B6%E6%9D%A5%E6%88%91%E7%9A%84%E5%8D%9A%E5%AE%A2/" title="欢迎来到CodeLong的博客！">
                            欢迎来到CodeLong的博客！</a>
                            <p>这是我第一次搭建博客！成功了，很开心！嘻嘻！以后我也会在博客上分享一些学习和生活上的感悟！希望大家来多多支持哦！...</p>
                        </div>
                        
                        <a href="https://codelong2010.github.io/posts/%E6%AC%A2%E8%BF%8E%E5%A4%A7%E5%AE%B6%E6%9D%A5%E6%88%91%E7%9A%84%E5%8D%9A%E5%AE%A2/" title="欢迎来到CodeLong的博客！">
                            <img class="relateimg" src="/img/face.jpg" alt="欢迎来到CodeLong的博客！">
                        </a>
                    </li>
                    
                
            
        </ul>
    </div>
</div>
<p style="text-align: center;">
  <a style="color: inherit" target="_blank" href="https://github.com/honjun/hugo-theme-diaspora"></a>
</p>

<script>
  var siteTitle = "Dragon Blog";
</script>
<script src="/js/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/diaspora.js"></script>
<script src="/js/custom.js"></script>
<script src="/js/InsightSearch.js"></script>
</body>
</html>

